package com.vision.mapper;

import com.vision.pojo.entity.UserFollow;
import com.vision.pojo.vo.UserFollowVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserFollowMapper {

    // 查询粉丝数
    @Select("select count(*) from vs_user_follow where user_id = #{id}")
    Integer selectFansCountByUserId(Integer id);

    /**
     * 查询关注数
     * @param userId 用户id
     * @return 关注数
     */
    List<UserFollowVO> selectFansForPage(Integer userId);

    // 查询关注列表
    @Select("select uf.id,uf.user_id,u.username,u.nickname,u.avatar from vs_user_follow uf left join vs_user u on uf.user_id = u.id where follower_id = #{userId}")
    List<UserFollowVO> selectSubscriptionForPage(Integer userId);

    // 删除粉丝
    @Delete("delete from vs_user_follow where user_id = #{userId} and follower_id = #{followerId}")
    void deleteFan(Integer userId, Integer followerId);

    // 关注
    @Insert("insert into vs_user_follow (user_id,follower_id,subscribe_time) values (#{targetUserId},#{userId},now())")
    void subscribe(Integer userId, Integer targetUserId);

    // 取消关注
    @Delete("delete from vs_user_follow where user_id = #{targetUserId} and follower_id = #{userId}")
    void cancelSubscribe(Integer userId, Integer targetUserId);

    // 查询是否关注
    @Select("select count(*) from vs_user_follow where user_id = #{targetUserId} and follower_id = #{currentUserId}")
    Integer selectCountByUserIdAndFollowId(Integer currentUserId, Integer targetUserId);

    /**
     * 根据用户id和关注id查询
     * @param userId 用户id
     * @param followerId 粉丝 ID
     * @return 关注
     */
    @Select("select * from vs_user_follow where user_id = #{userId} and follower_id = #{followerId}")
    UserFollow selectByUserIdAndFollowId(Integer userId, Integer followerId);
}
